package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import spring.UUIDResourcesHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@SpringBootApplication
@Controller
public class Application {


    /**
     * 将spring 部分代码独立出来，下载使用，Spring mvc 4.3以前功能不完善
     * @param request
     * @param response
     */
    @RequestMapping("/source/standalone")
    public void aloneDownload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        File file = new ClassPathResource("hello.txt").getFile();
       standalone.WebPointLoad.pointLoad(response,request,file,"text/plan");
    }

    /**
     * spring 处理下载 Spring mvc 4.3适合使用
     * @param request
     * @param response
     */
    @RequestMapping("/source/spring")
    public void springDownload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        File file = new ClassPathResource("hello.txt").getFile();
        spring.WebPointLoad.pointLoad(response,request,file,"text/plan");
    }


    @Configuration
    public static  class WebMcvCfg extends WebMvcConfigurerAdapter {
        @Bean
        UUIDResourcesHandler handler(){
            UUIDResourcesHandler handler = new UUIDResourcesHandler();
            return  handler;
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
